package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.Article;
import com.baizhi.service.ArticleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //展示
    @LogAnnotation(content = "查询文章",type = "select")
    @ResponseBody
    @RequestMapping("show")
    public Map show(int page,int limit,String name){
        Map map = new HashMap();
        PageInfo<Article> pageInfo = articleService.showAll(page, limit,name);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return  map;
    }

    //添加
    @LogAnnotation(content = "添加文章",type = "add")
    @ResponseBody
    @RequestMapping("add")
    public Map add(Article article){
        Map map = new HashMap();
        try {
            articleService.add(article);
            map.put("isAdd",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd",false);
        }
        return map;
    }


    //上传文件
    @ResponseBody
    @RequestMapping("upload")
    public Map upload(MultipartFile file) throws IOException {
        Map map = new HashMap();
        String s1 = UUID.randomUUID().toString() + file.getOriginalFilename();
        file.transferTo(new File("G:\\JAVA\\IDEAWorkspace\\cmfzdemo\\src\\main\\webapp\\si",s1));
        map.put("image","/si/"+s1);
        System.out.println("s1 = " + s1);
        return map;
    }



}
